<!--版权所有 (C) 2025 尋鯨錄 -->
<!--保留所有权利。 -->
<!--博客：https://www.xunjinlu.fun/ -->
<!--如需二开请保留版权，谢谢。 -->

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片库 - 简单图床</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f5f5;
            color: #333;
        }

        .header {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            background: white;
            padding: clamp(0.5rem, 2vw, 1rem);
            text-align: center;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            z-index: 100;
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 10px;
        }

        .header-left {
            display: flex;
            align-items: center;
            flex-shrink: 0;
        }

        .header h1 {
            color: #4a90e2;
            font-size: clamp(0.9rem, 2.5vw, 1.2rem);
            margin: 0;
            white-space: nowrap;
        }

        .back-btn {
            color: #4a90e2;
            text-decoration: none;
            font-size: clamp(0.8rem, 2.2vw, 1rem);
            margin-right: clamp(0.5rem, 1.5vw, 1rem);
            white-space: nowrap;
        }

        .header-right {
            display: flex;
            gap: clamp(5px, 1.5vw, 10px);
            align-items: center;
            flex-wrap: wrap;
            justify-content: flex-end;
            flex-shrink: 0;
        }

        .sort-btn, .manage-btn {
            padding: clamp(6px, 1.5vw, 8px) clamp(12px, 2.5vw, 16px);
            background: #4a90e2;
            color: white;
            border: none;
            border-radius: clamp(3px, 1vw, 5px);
            cursor: pointer;
            font-size: clamp(11px, 2.5vw, 14px);
            transition: all 0.3s ease;
            white-space: nowrap;
            min-width: 0;
            flex-shrink: 0;
            width: clamp(60px, 15vw, 80px);
            text-align: center;
        }

        .sort-btn:hover, .manage-btn:hover {
            background: #357abd;
        }

        .sort-btn.active {
            background: #28a745;
        }

        .manage-btn.active {
            background: #dc3545;
        }

        .gallery-container {
            margin-top: 80px;
            padding: 10px;
        }

        /* 布局选择器 */
        .layout-selector {
            position: relative;
            z-index: 150;
        }

        .layout-dropdown {
            position: absolute;
            top: 100%;
            right: 0;
            background: white;
            border: 1px solid #ddd;
            border-radius: clamp(3px, 1vw, 5px);
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            display: none;
            z-index: 200;
            min-width: clamp(100px, 25vw, 120px);
            padding: clamp(3px, 1vw, 5px);
        }

        .layout-dropdown.show {
            display: block;
        }

        .layout-btn {
            padding: clamp(6px, 1.5vw, 8px) clamp(12px, 2.5vw, 16px);
            background: #4a90e2;
            color: white;
            border: none;
            border-radius: clamp(3px, 1vw, 5px);
            cursor: pointer;
            font-size: clamp(11px, 2.5vw, 14px);
            transition: all 0.3s ease;
            white-space: nowrap;
            min-width: 0;
            flex-shrink: 0;
        }

        .layout-btn:hover {
            background: #357abd;
        }

        .layout-option {
            padding: clamp(6px, 1.5vw, 8px) clamp(10px, 2vw, 12px);
            cursor: pointer;
            border-bottom: 1px solid #eee;
            transition: background 0.3s ease;
            font-size: clamp(10px, 2.5vw, 12px);
            white-space: nowrap;
        }

        .layout-option:last-child {
            border-bottom: none;
        }

        .layout-option:hover {
            background: #f8f9fa;
        }

        .layout-option.active {
            background: #4a90e2;
            color: white;
        }

        /* 不同布局样式 */
        .gallery-container.columns-2 {
            column-count: 2;
            column-gap: 10px;
        }

        .gallery-container.columns-3 {
            column-count: 3;
            column-gap: 10px;
        }

        .gallery-container.columns-4 {
            column-count: 4;
            column-gap: 10px;
        }

        .gallery-container.grid {
            column-count: unset;
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 10px;
        }

        .gallery-item {
            break-inside: avoid;
            margin-bottom: 10px;
            position: relative;
        }



        /* 图片样式 */
        .gallery-item img {
            width: 100%;
            height: auto;
            display: block;
            border-radius: 8px;
            cursor: pointer;
            opacity: 1;
            transition: transform 0.2s ease;
        }

        .gallery-item img:hover {
            transform: scale(1.02);
        }

        /* 管理模式下的操作按钮 */
        .item-actions {
            position: absolute;
            top: 5px;
            right: 5px;
            display: none;
            gap: 5px;
            z-index: 10;
        }

        .manage-mode .item-actions {
            display: flex;
        }

        /* 确保管理模式下图片项有足够的空间显示操作按钮 */
        .manage-mode .gallery-item {
            position: relative;
            overflow: visible;
        }

        .manage-mode .gallery-item img {
            transition: filter 0.3s ease;
        }

        .manage-mode .gallery-item:hover img {
            filter: brightness(0.8);
        }

        .action-btn {
            width: clamp(25px, 6vw, 30px);
            height: clamp(25px, 6vw, 30px);
            border: none;
            border-radius: 50%;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: clamp(10px, 2.5vw, 12px);
            transition: all 0.3s ease;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
            backdrop-filter: blur(10px);
        }

        .delete-btn {
            background: #dc3545;
            color: white;
        }

        .replace-btn {
            background: #ffc107;
            color: #333;
        }

        .action-btn:hover {
            transform: scale(1.1);
        }

        /* 排序下拉菜单 */
        .sort-dropdown {
            position: absolute;
            top: 100%;
            right: 0;
            background: white;
            border: 1px solid #ddd;
            border-radius: clamp(3px, 1vw, 5px);
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            display: none;
            z-index: 200;
            min-width: clamp(120px, 30vw, 150px);
        }

        .sort-dropdown.show {
            display: block;
        }

        .sort-option {
            padding: clamp(8px, 2vw, 10px) clamp(12px, 2.5vw, 15px);
            cursor: pointer;
            border-bottom: 1px solid #eee;
            transition: background 0.3s ease;
            font-size: clamp(11px, 2.5vw, 14px);
            white-space: nowrap;
        }

        .sort-option:last-child {
            border-bottom: none;
        }

        .sort-option:hover {
            background: #f8f9fa;
        }

        .sort-option.active {
            background: #4a90e2;
            color: white;
        }

        /* 图片详情模态框 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.9);
            z-index: 1000;
            overflow-y: auto;
            padding: 20px;
            box-sizing: border-box;
        }

        .modal-content {
            position: relative;
            background: white;
            margin: 0 auto;
            padding: 20px;
            max-width: 800px;
            width: 100%;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
            animation: modalSlideIn 0.3s ease-out;
        }

        @keyframes modalSlideIn {
            from {
                opacity: 0;
                transform: translateY(-50px) scale(0.9);
            }
            to {
                opacity: 1;
                transform: translateY(0) scale(1);
            }
        }

        .modal-close {
            position: absolute;
            right: 15px;
            top: 15px;
            font-size: 28px;
            color: #666;
            cursor: pointer;
            width: 30px;
            height: 30px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.9);
            transition: all 0.3s ease;
            z-index: 1001;
        }

        .modal-close:hover {
            background: rgba(255, 255, 255, 1);
            color: #333;
            transform: scale(1.1);
        }

        .modal-image {
            max-width: 100%;
            max-height: 60vh;
            width: auto;
            height: auto;
            border-radius: 8px;
            margin-bottom: 20px;
            display: block;
            margin-left: auto;
            margin-right: auto;
            object-fit: contain;
        }

        .modal-image-container {
            max-height: 60vh;
            overflow-y: auto;
            border-radius: 8px;
            margin-bottom: 20px;
            background: #f8f9fa;
            padding: 10px;
            text-align: center;
            border: 1px solid #e9ecef;
        }

        /* 自定义滚动条样式 */
        .modal-image-container::-webkit-scrollbar {
            width: 8px;
        }

        .modal-image-container::-webkit-scrollbar-track {
            background: #f1f1f1;
            border-radius: 4px;
        }

        .modal-image-container::-webkit-scrollbar-thumb {
            background: #c1c1c1;
            border-radius: 4px;
        }

        .modal-image-container::-webkit-scrollbar-thumb:hover {
            background: #a8a8a8;
        }

        .modal-info {
            padding: 15px;
            background: #f8f9fa;
            border-radius: 8px;
        }

        .modal-info h3 {
            margin-bottom: 15px;
            color: #333;
        }

        .info-item {
            margin-bottom: 10px;
        }

        .url-container {
            display: flex;
            gap: 10px;
            margin-top: 15px;
        }

        .url-input {
            flex: 1;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 14px;
        }

        .copy-btn {
            padding: 10px 20px;
            background: #4a90e2;
            color: white;
            border: none;
            border-radius: 6px;
            cursor: pointer;
            white-space: nowrap;
        }

        .copy-btn:hover {
            background: #357abd;
        }

        /* 重传模态框 */
        .replace-modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.9);
            z-index: 1000;
        }

        .replace-modal-content {
            position: relative;
            background: white;
            margin: 100px auto;
            padding: 30px;
            max-width: 500px;
            border-radius: 12px;
        }

        .replace-modal h3 {
            margin-bottom: 20px;
            color: #333;
        }

        .replace-form {
            display: flex;
            flex-direction: column;
            gap: 15px;
        }

        .replace-file-input {
            padding: 10px;
            border: 2px dashed #4a90e2;
            border-radius: 5px;
            text-align: center;
            cursor: pointer;
        }

        .replace-buttons {
            display: flex;
            gap: 10px;
            justify-content: flex-end;
        }

        .cancel-btn {
            padding: 10px 20px;
            background: #6c757d;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        .confirm-btn {
            padding: 10px 20px;
            background: #28a745;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        /* 认证按钮样式 */
        .auth-btn {
            padding: clamp(6px, 1.5vw, 8px) clamp(12px, 2.5vw, 16px);
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: clamp(15px, 4vw, 25px);
            cursor: pointer;
            font-size: clamp(10px, 2.5vw, 13px);
            font-weight: 500;
            transition: all 0.3s ease;
            box-shadow: 0 2px 10px rgba(102, 126, 234, 0.3);
            position: relative;
            overflow: hidden;
            white-space: nowrap;
            min-width: 0;
            flex-shrink: 0;
        }

        .auth-btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
            transition: left 0.5s;
        }

        .auth-btn:hover::before {
            left: 100%;
        }

        .auth-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 20px rgba(102, 126, 234, 0.4);
        }

        .auth-btn.logged-in {
            background: linear-gradient(135deg, #56ab2f 0%, #a8e6cf 100%);
            box-shadow: 0 2px 10px rgba(86, 171, 47, 0.3);
        }

        .auth-btn.logged-in:hover {
            box-shadow: 0 4px 20px rgba(86, 171, 47, 0.4);
        }

        /* 认证模态框样式 */
        .auth-modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0, 0, 0, 0.8);
            z-index: 1000;
            backdrop-filter: blur(5px);
        }

        .auth-modal-content {
            position: relative;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            margin: 80px auto;
            padding: 40px;
            max-width: 420px;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
            animation: authModalSlideIn 0.4s ease-out;
        }

        @keyframes authModalSlideIn {
            from {
                opacity: 0;
                transform: translateY(-30px) scale(0.9);
            }
            to {
                opacity: 1;
                transform: translateY(0) scale(1);
            }
        }

        .auth-modal h3 {
            color: #2c3e50;
            font-size: 24px;
            margin-bottom: 10px;
            text-align: center;
            font-weight: 600;
        }

        .auth-modal p {
            color: #7f8c8d;
            text-align: center;
            margin-bottom: 25px;
            font-size: 14px;
        }

        .auth-form {
            display: flex;
            flex-direction: column;
            gap: 20px;
        }

        .auth-input {
            padding: 15px 20px;
            border: 2px solid #e0e6ed;
            border-radius: 12px;
            font-size: 16px;
            background: white;
            transition: all 0.3s ease;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
        }

        .auth-input:focus {
            outline: none;
            border-color: #667eea;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
            transform: translateY(-1px);
        }

        .auth-buttons {
            display: flex;
            gap: 15px;
            justify-content: center;
            margin-top: 10px;
        }

        .auth-buttons .cancel-btn {
            background: linear-gradient(135deg, #95a5a6 0%, #7f8c8d 100%);
            border-radius: 12px;
            padding: 12px 25px;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .auth-buttons .confirm-btn {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 12px;
            padding: 12px 25px;
            font-weight: 500;
            transition: all 0.3s ease;
        }

        .auth-buttons button:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
        }

        @media (max-width: 768px) {
            /* 手机端保持双列布局，不降级为单列 */
            .gallery-container.columns-2 {
                column-count: 2;
                column-gap: 8px;
            }
            
            .gallery-container.columns-3 {
                column-count: 2;
                column-gap: 8px;
            }
            
            .gallery-container.columns-4 {
                column-count: 2;
                column-gap: 8px;
            }
            
            .gallery-container.grid {
                grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
                gap: 8px;
            }

            /* 小屏幕设备上的顶部栏优化 */
            .header {
                padding: clamp(0.3rem, 1.5vw, 0.5rem);
                gap: 5px;
            }

            .header-right {
                gap: 3px;
            }

            .sort-btn, .manage-btn, .layout-btn, .auth-btn {
                padding: clamp(4px, 1vw, 6px) clamp(8px, 2vw, 12px);
                font-size: clamp(9px, 2vw, 11px);
                width: clamp(50px, 12vw, 60px);
            }

            .sort-dropdown, .layout-dropdown {
                min-width: clamp(80px, 20vw, 100px);
            }

            .sort-option, .layout-option {
                padding: clamp(5px, 1.5vw, 6px) clamp(8px, 2vw, 10px);
                font-size: clamp(9px, 2vw, 10px);
            }

            .gallery-container {
                padding: 5px;
            }

            .gallery-item {
                margin-bottom: 5px;
            }

            .modal {
                padding: 10px;
            }

            .modal-content {
                margin: 0;
                padding: 15px;
                max-width: 100%;
            }

            .modal-image {
                max-height: 50vh;
            }

            .modal-image-container {
                max-height: 50vh;
            }

            /* 小屏幕设备上的操作按钮优化 */
            .action-btn {
                width: clamp(22px, 5vw, 25px);
                height: clamp(22px, 5vw, 25px);
                font-size: clamp(9px, 2vw, 10px);
            }
        }

        @media (max-width: 480px) {
            .header {
                flex-direction: row;
                gap: 5px;
                padding: clamp(0.2rem, 1vw, 0.3rem);
                align-items: center;
                justify-content: space-between;
            }

            .header-left {
                order: 1;
                flex-shrink: 0;
            }

            .header-right {
                order: 2;
                position: relative;
                justify-content: flex-end;
                flex-shrink: 0;
                flex-wrap: wrap;
                gap: 3px;
            }

            .header h1 {
                font-size: clamp(0.8rem, 2.2vw, 1rem);
            }

            .back-btn {
                font-size: clamp(0.6rem, 1.6vw, 0.7rem);
            }

            .sort-btn, .manage-btn, .layout-btn, .auth-btn {
                padding: clamp(3px, 1vw, 4px) clamp(6px, 1.5vw, 8px);
                font-size: clamp(8px, 1.8vw, 9px);
                width: clamp(40px, 8vw, 45px);
                min-width: 0;
                flex-shrink: 0;
            }

            .sort-dropdown, .layout-dropdown {
                min-width: clamp(70px, 18vw, 80px);
            }

            .sort-option, .layout-option {
                padding: clamp(4px, 1vw, 5px) clamp(6px, 1.5vw, 8px);
                font-size: clamp(8px, 1.8vw, 9px);
            }

            /* 超小屏幕设备上的操作按钮优化 */
            .action-btn {
                width: clamp(20px, 4.5vw, 22px);
                height: clamp(20px, 4.5vw, 22px);
                font-size: clamp(8px, 1.8vw, 9px);
            }
        }

        /* 超小屏幕设备上的额外优化 */
        @media (max-width: 360px) {
            .header {
                padding: clamp(0.15rem, 0.8vw, 0.2rem);
                gap: 3px;
            }

            .header h1 {
                font-size: clamp(0.7rem, 2vw, 0.9rem);
            }

            .back-btn {
                font-size: clamp(0.5rem, 1.4vw, 0.6rem);
                margin-right: clamp(0.3rem, 1vw, 0.5rem);
            }

            .sort-btn, .manage-btn, .layout-btn, .auth-btn {
                padding: clamp(2px, 0.8vw, 3px) clamp(4px, 1.2vw, 6px);
                font-size: clamp(7px, 1.6vw, 8px);
                width: clamp(35px, 7vw, 40px);
            }

            .sort-dropdown, .layout-dropdown {
                min-width: clamp(60px, 15vw, 70px);
            }

            .sort-option, .layout-option {
                padding: clamp(3px, 0.8vw, 4px) clamp(4px, 1.2vw, 6px);
                font-size: clamp(7px, 1.6vw, 8px);
            }

            .action-btn {
                width: clamp(18px, 4vw, 20px);
                height: clamp(18px, 4vw, 20px);
                font-size: clamp(7px, 1.6vw, 8px);
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <div class="header-left">
        <a href="index.html" class="back-btn">
            <i class="fas fa-arrow-left"></i>
        </a>
        <h1>图片库</h1>
    </div>
        <div class="header-right">
            <div class="sort-container" style="position: relative;">
                <button class="sort-btn" onclick="toggleSortDropdown()">
                    <i class="fas fa-sort"></i> 排序
                </button>
                <div class="sort-dropdown" id="sortDropdown">
                    <div class="sort-option" data-sort="name-asc" onclick="sortImages('name-asc')">
                        <i class="fas fa-sort-numeric-down"></i> 序号 小到大
                    </div>
                    <div class="sort-option" data-sort="name-desc" onclick="sortImages('name-desc')">
                        <i class="fas fa-sort-numeric-up"></i> 序号 大到小
                    </div>
                    <div class="sort-option" data-sort="size-asc" onclick="sortImages('size-asc')">
                        <i class="fas fa-sort-amount-down"></i> 大小 小到大
                    </div>
                    <div class="sort-option" data-sort="size-desc" onclick="sortImages('size-desc')">
                        <i class="fas fa-sort-amount-up"></i> 大小 大到小
                    </div>
                    <div class="sort-option" data-sort="time-asc" onclick="sortImages('time-asc')">
                        <i class="fas fa-clock"></i> 时间 早到晚
                    </div>
                    <div class="sort-option" data-sort="time-desc" onclick="sortImages('time-desc')">
                        <i class="fas fa-clock"></i> 时间 晚到早
                    </div>
                </div>
            </div>
            <div class="layout-selector">
                <button class="layout-btn" onclick="toggleLayoutDropdown()">
                    <i class="fas fa-th-large"></i> 布局
                </button>
                <div class="layout-dropdown" id="layoutDropdown">
                    <div class="layout-option" data-layout="columns-2" onclick="changeLayout('columns-2')">
                        <i class="fas fa-columns"></i> 双列
                    </div>
                    <div class="layout-option" data-layout="columns-3" onclick="changeLayout('columns-3')">
                        <i class="fas fa-columns"></i> 三列
                    </div>
                    <div class="layout-option" data-layout="columns-4" onclick="changeLayout('columns-4')">
                        <i class="fas fa-columns"></i> 四列
                    </div>
                    <div class="layout-option" data-layout="grid" onclick="changeLayout('grid')">
                        <i class="fas fa-th"></i> 网格
                    </div>
                </div>
            </div>
            <button class="manage-btn" onclick="toggleManageMode()">
                <i class="fas fa-cog"></i> 管理
            </button>
            <button class="auth-btn" id="authBtn" onclick="toggleAuthModal()">
                <i class="fas fa-user"></i> <span id="authStatus">登录</span>
            </button>
        </div>
    </div>



    <div class="gallery-container columns-2" id="gallery">
        <!-- 图片将通过 JavaScript 动态加载 -->
    </div>

    <!-- 图片详情模态框 -->
    <div class="modal" id="imageModal">
        <div class="modal-content">
            <span class="modal-close" onclick="closeModal()">&times;</span>
            <div class="modal-image-container">
            <img class="modal-image" id="modalImage">
            </div>
            <div class="modal-info">
                <h3>图片信息</h3>
                <div class="info-item">文件名：<span id="modalFileName"></span></div>
                <div class="info-item">类型：<span id="modalFileType"></span></div>
                <div class="info-item">大小：<span id="modalFileSize"></span></div>
                <div class="info-item">上传时间：<span id="modalFileTime"></span></div>
                <div class="url-container">
                    <input type="text" class="url-input" id="modalImageUrl" readonly>
                    <button class="copy-btn" onclick="copyImageUrl()">
                        <i class="fas fa-copy"></i> 复制链接
                    </button>
                </div>
            </div>
        </div>
    </div>

    <!-- 重传模态框 -->
    <div class="replace-modal" id="replaceModal">
        <div class="replace-modal-content">
            <span class="modal-close" onclick="closeReplaceModal()">&times;</span>
            <h3>重传图片</h3>
            <div class="replace-form">
                <div class="replace-file-input" onclick="document.getElementById('replaceFileInput').click()">
                    <i class="fas fa-cloud-upload-alt"></i><br>
                    点击选择新图片
                </div>
                <input type="file" id="replaceFileInput" accept="image/*" style="display: none;" onchange="handleReplaceFileSelect()">
                <div class="replace-buttons">
                    <button class="cancel-btn" onclick="closeReplaceModal()">取消</button>
                    <button class="confirm-btn" onclick="confirmReplace()">确认重传</button>
                </div>
            </div>
        </div>
    </div>

    <!-- 登录模态框 -->
    <div class="auth-modal" id="authModal">
        <div class="auth-modal-content">
            <span class="modal-close" onclick="closeAuthModal()">&times;</span>
            <div id="loginForm">
                <h3>用户登录</h3>
                <p style="color: #666; margin-bottom: 20px;">请输入管理员密码</p>
                <div class="auth-form">
                    <input type="password" id="passwordInput" placeholder="请输入密码" class="auth-input">
                    <div class="auth-buttons">
                        <button class="cancel-btn" onclick="closeAuthModal()">取消</button>
                        <button class="confirm-btn" onclick="handleLogin()">登录</button>
                    </div>
                </div>
            </div>
            <div id="userInfo" style="display: none;">
                <h3>用户信息</h3>
                <p style="color: #666; margin-bottom: 20px;">您已登录</p>
                <div class="auth-buttons">
                    <button class="confirm-btn" onclick="handleLogout()">退出登录</button>
                </div>
            </div>
        </div>
    </div>

    <script>
        let currentImages = [];
        let currentSort = 'name-asc';
        let isManageMode = false;
        let selectedReplaceFile = null;
        let currentReplaceFileName = null;
        let currentLayout = window.innerWidth <= 768 ? 'columns-2' : 'grid';
        let authStatus = { enabled: false, logged_in: false };

        // 加载图片库
        function loadGallery() {
            console.log('开始加载图片库...');
            
            fetch('list_images.php')
                .then(response => {
                    console.log('收到响应:', response);
                    return response.json();
                })
                .then(images => {
                    console.log('解析到的图片数据:', images);
                    currentImages = images;
                    displayImages();
                })
                .catch(error => {
                    console.error('Error loading gallery:', error);
                    document.getElementById('gallery').innerHTML = '<p style="text-align: center; padding: 20px; color: red;">加载图片失败: ' + error.message + '</p>';
                });
        }

        // 显示图片（简化版本）
        function displayImages() {
            console.log('开始显示图片，共', currentImages.length, '张');
                    const gallery = document.getElementById('gallery');
            
            if (currentImages.length === 0) {
                gallery.innerHTML = '<p style="text-align: center; padding: 20px; color: #666;">暂无图片</p>';
                return;
            }
            
            // 清空画廊
                    gallery.innerHTML = '';
            
            // 直接显示所有图片
            currentImages.forEach((image, index) => {
                        const item = document.createElement('div');
                        item.className = 'gallery-item';
                        item.innerHTML = `
                    <img src="img/${image.filename}" alt="${image.filename}" 
                         onclick="showImageDetails('${image.filename}')"
                         onload="console.log('图片加载成功:', '${image.filename}')"
                         onerror="console.error('图片加载失败:', '${image.filename}')">
                    <div class="item-actions">
                        <button class="action-btn delete-btn" onclick="deleteImage('${image.filename}')" title="删除图片">
                            <i class="fas fa-trash"></i>
                        </button>
                        <button class="action-btn replace-btn" onclick="showReplaceModal('${image.filename}')" title="重传图片">
                            <i class="fas fa-upload"></i>
                        </button>
                    </div>
                        `;
                        gallery.appendChild(item);
                    });
            
            console.log('图片显示完成');
        }



        // 排序图片
        function sortImages(sortType) {
            currentSort = sortType;
            
            // 更新排序按钮状态
            document.querySelectorAll('.sort-option').forEach(option => {
                option.classList.remove('active');
            });
            document.querySelector(`[data-sort="${sortType}"]`).classList.add('active');
            
            // 关闭下拉菜单
            document.getElementById('sortDropdown').classList.remove('show');
            
            // 从服务器获取排序后的数据
            fetch(`list_images.php?sort=${sortType}`)
                .then(response => response.json())
                .then(images => {
                    currentImages = images;
                    displayImages();
                })
                .catch(error => console.error('Error sorting images:', error));
        }

        // 切换布局
        function changeLayout(layout) {
            currentLayout = layout;
            
            // 更新布局选项状态
            document.querySelectorAll('.layout-option').forEach(option => {
                option.classList.remove('active');
            });
            document.querySelector(`[data-layout="${layout}"]`).classList.add('active');
            
            // 关闭下拉菜单
            document.getElementById('layoutDropdown').classList.remove('show');
            
            // 更新画廊容器类名
            const gallery = document.getElementById('gallery');
            gallery.className = `gallery-container ${layout}`;
            
            console.log('布局已切换到:', layout);
            console.log('画廊容器类名:', gallery.className);
            
            // 重新加载图片以应用新布局
            if (currentImages.length > 0) {
                displayImages();
            }
        }



        // 切换排序下拉菜单
        function toggleSortDropdown() {
            const dropdown = document.getElementById('sortDropdown');
            dropdown.classList.toggle('show');
        }

        // 切换布局下拉菜单
        function toggleLayoutDropdown() {
            const dropdown = document.getElementById('layoutDropdown');
            dropdown.classList.toggle('show');
        }

        // 切换管理模式
        function toggleManageMode() {
            isManageMode = !isManageMode;
            const manageBtn = document.querySelector('.manage-btn');
            const gallery = document.getElementById('gallery');
            
            if (isManageMode) {
                manageBtn.classList.add('active');
                manageBtn.innerHTML = '<i class="fas fa-times"></i> 退出管理';
                gallery.classList.add('manage-mode');
            } else {
                manageBtn.classList.remove('active');
                manageBtn.innerHTML = '<i class="fas fa-cog"></i> 管理';
                gallery.classList.remove('manage-mode');
            }
        }

        // 删除图片
        function deleteImage(filename) {
            if (confirm(`确定要删除图片 "${filename}" 吗？`)) {
                fetch('delete_image.php', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ filename: filename })
                })
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        alert('图片删除成功！');
                        loadGallery(); // 重新加载图片库
                    } else {
                        alert('删除失败：' + data.error);
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('删除失败，请重试');
                });
            }
        }

        // 显示重传模态框
        function showReplaceModal(filename) {
            currentReplaceFileName = filename;
            document.getElementById('replaceModal').style.display = 'block';
        }

        // 关闭重传模态框
        function closeReplaceModal() {
            document.getElementById('replaceModal').style.display = 'none';
            selectedReplaceFile = null;
            currentReplaceFileName = null;
            document.getElementById('replaceFileInput').value = '';
        }

        // 处理重传文件选择
        function handleReplaceFileSelect() {
            const fileInput = document.getElementById('replaceFileInput');
            selectedReplaceFile = fileInput.files[0];
            
            if (selectedReplaceFile) {
                const fileInputDiv = document.querySelector('.replace-file-input');
                fileInputDiv.innerHTML = `
                    <i class="fas fa-check"></i><br>
                    已选择: ${selectedReplaceFile.name}
                `;
            }
        }

        // 确认重传
        function confirmReplace() {
            if (!selectedReplaceFile) {
                alert('请先选择要重传的图片');
                return;
            }

            const formData = new FormData();
            formData.append('image', selectedReplaceFile);
            formData.append('replace_filename', currentReplaceFileName);

            fetch('replace_image.php', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert('图片重传成功！');
                    closeReplaceModal();
                    loadGallery(); // 重新加载图片库
                } else {
                    alert('重传失败：' + data.error);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('重传失败，请重试');
            });
        }

        // 显示图片详情
        function showImageDetails(imageName) {
            const modal = document.getElementById('imageModal');
            const modalImage = document.getElementById('modalImage');
            const modalFileName = document.getElementById('modalFileName');
            const modalFileType = document.getElementById('modalFileType');
            const modalFileSize = document.getElementById('modalFileSize');
            const modalFileTime = document.getElementById('modalFileTime');
            const modalImageUrl = document.getElementById('modalImageUrl');

            const imageUrl = `${window.location.origin}${window.location.pathname.replace('gallery.html', '')}img/${imageName}`;
            
            modalImage.src = `img/${imageName}`;
            modalFileName.textContent = imageName;
            modalFileType.textContent = imageName.split('.').pop().toUpperCase();
            
            // 获取文件信息
            fetch(`get_file_info.php?filename=${imageName}`)
                .then(response => response.json())
                .then(data => {
                    modalFileSize.textContent = data.size || '未知';
                    modalFileTime.textContent = data.time || '未知';
                })
                .catch(error => {
                    modalFileSize.textContent = '未知';
                    modalFileTime.textContent = '未知';
                });
            
            modalImageUrl.value = imageUrl;
            modal.style.display = 'block';
            document.body.style.overflow = 'hidden';
        }

        // 关闭模态框
        function closeModal() {
            const modal = document.getElementById('imageModal');
            modal.style.display = 'none';
            document.body.style.overflow = 'auto';
        }

        // 复制图片链接
        function copyImageUrl() {
            const urlInput = document.getElementById('modalImageUrl');
            urlInput.select();
            document.execCommand('copy');
            alert('链接已复制到剪贴板！');
        }

        // 点击模态框外部关闭
        window.onclick = function(event) {
            const modal = document.getElementById('imageModal');
            const replaceModal = document.getElementById('replaceModal');
            if (event.target == modal) {
                closeModal();
            }
            if (event.target == replaceModal) {
                closeReplaceModal();
            }
        }

        // 点击其他地方关闭下拉菜单
        document.addEventListener('click', function(event) {
            const sortContainer = document.querySelector('.sort-container');
            const layoutSelector = document.querySelector('.layout-selector');
            
            if (!sortContainer.contains(event.target)) {
                document.getElementById('sortDropdown').classList.remove('show');
            }
            
            if (!layoutSelector.contains(event.target)) {
                document.getElementById('layoutDropdown').classList.remove('show');
            }
        });

        // 页面加载完成后加载图片库
        document.addEventListener('DOMContentLoaded', function() {
            // 根据屏幕宽度设置默认布局
            const screenWidth = window.innerWidth;
            if (screenWidth <= 768) {
                currentLayout = 'columns-2';
            } else {
                currentLayout = 'grid';
            }
            
            // 应用默认布局
            const gallery = document.getElementById('gallery');
            gallery.className = `gallery-container ${currentLayout}`;
            
            // 设置默认布局选项为激活状态
            document.querySelector(`[data-layout="${currentLayout}"]`)?.classList.add('active');
            
            // 检查认证状态
            checkAuthStatus();
            
            loadGallery();
            
            // 添加窗口大小变化监听
            window.addEventListener('resize', function() {
                // 重新计算布局
                if (currentImages.length > 0) {
                    const gallery = document.getElementById('gallery');
                    gallery.className = `gallery-container ${currentLayout}`;
                }
            });
        });

        // 认证相关函数
        function checkAuthStatus() {
            fetch('auth_api.php?action=status')
                .then(response => response.json())
                .then(data => {
                    if (data.success) {
                        authStatus = data.data;
                        updateAuthUI();
                        // 根据认证状态显示或隐藏管理按钮
                        updateManageButtonVisibility();
                    }
                })
                .catch(error => console.error('检查认证状态失败:', error));
        }

        function updateAuthUI() {
            const authBtn = document.getElementById('authBtn');
            const authStatusSpan = document.getElementById('authStatus');
            
            if (authStatus.enabled) {
                if (authStatus.logged_in) {
                    authBtn.classList.add('logged-in');
                    authStatusSpan.textContent = '已登录';
                } else {
                    authBtn.classList.remove('logged-in');
                    authStatusSpan.textContent = '登录';
                }
            } else {
                authBtn.style.display = 'none';
            }
        }

        function updateManageButtonVisibility() {
            const manageBtn = document.querySelector('.manage-btn');
            if (manageBtn) {
                if (authStatus.enabled && authStatus.logged_in) {
                    manageBtn.style.display = 'inline-block';
                } else {
                    manageBtn.style.display = 'none';
                }
            }
        }

        function toggleAuthModal() {
            if (!authStatus.enabled) return;
            
            const modal = document.getElementById('authModal');
            const loginForm = document.getElementById('loginForm');
            const userInfo = document.getElementById('userInfo');
            
            if (authStatus.logged_in) {
                loginForm.style.display = 'none';
                userInfo.style.display = 'block';
            } else {
                loginForm.style.display = 'block';
                userInfo.style.display = 'none';
                document.getElementById('passwordInput').value = '';
            }
            
            modal.style.display = 'block';
        }

        function closeAuthModal() {
            document.getElementById('authModal').style.display = 'none';
        }

        function handleLogin() {
            const password = document.getElementById('passwordInput').value;
            
            if (!password) {
                alert('请输入密码');
                return;
            }
            
            fetch('auth_api.php?action=login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ password: password })
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert(data.message);
                    closeAuthModal();
                    // 立即更新认证状态和UI
                    authStatus.logged_in = true;
                    updateAuthUI();
                    updateManageButtonVisibility();
                } else {
                    alert('登录失败: ' + data.error);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('登录失败，请重试');
            });
        }

        function handleLogout() {
            fetch('auth_api.php?action=logout', {
                method: 'POST'
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert(data.message);
                    closeAuthModal();
                    // 立即更新认证状态和UI
                    authStatus.logged_in = false;
                    updateAuthUI();
                    updateManageButtonVisibility();
                } else {
                    alert('退出失败: ' + data.error);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('退出失败，请重试');
            });
        }
    </script>
</body>
</html> 